export interface WebsiteConfiguration {
  logo: string;
  name: string;
  description: string;
  url: string;
  version: string;
  copyright: string;
  setUpTime: string | Date;
  startRunningTime: string | Date;
  ICP备案: string;
  公网安备案: string;
  萌ICP备案号: string;
}
export interface EmailConfiguration {
  enable: boolean;
  host: string;
  port: number;
  secure: boolean; // Use `true` for port 465, `false` for all other ports
  user: string;
  pass: string;
}
export interface UserConfiguration {
  // 管理员用户名
  administrators: string[];
  // 站长信息
  master: {
    nickname: string | undefined;
    username: string | undefined;
    emails: string[];
  };
}
export interface ArticleConfiguration {
  // 归档数据更新时间间隔（分钟）
  archiveUpdateInterval: number;
  // 点赞功能
  enableLike: boolean;
  // 评论功能
  enableComment: boolean;
  enableCommentReview: boolean;
}
export interface RSSConfiguration {
  enable: boolean;
  // RSS文件更新时间间隔（分钟）
  updateInterval: number;
}
// twikoo 评论系统配置
// 参考 https://twikoo.js.org/frontend.html
export interface TwikooConfiguration {
  enable: boolean;
  envID: string;
  region: "ap-shanghai" | "ap-guangzhou" | "";
  lang: "zh" | "zh-cn" | "zh-hk" | "zh-tw" | "en" | "en-us" | "uz" | "ja";
}
// 百度统计相关配置
export interface BaiduTongjiConfiguration {
  domain: string;
  code: string;
}
// 微信公众号API相关配置
export interface WechatConfiguration {
  appID: string;
  appSecret: string;
}
// 服务相关配置
export interface ServerConfiguration {
  port: number;
  https: {
    enable: boolean;
    http2: boolean;
    certs: {
      // 带 default 则 匹配不到时默认使用
      servername: string[];
      cert: string;
      key: string;
    }[];
  };
}
// 内容资源相关
export interface ResourceConfiguration {
  rootDirectoryPath: string;
  imageDirectoryPath: string;
  videoDirectoryPath: string;
  audioDirectoryPath: string;
  articleDirectoryPath: string;
  RSSDirectoryPath: string;
  RSSFilePath: string;
}

export interface Configuration {
  // 网站相关配置
  website: WebsiteConfiguration;
  email: EmailConfiguration;
  user: UserConfiguration;
  article: ArticleConfiguration;
  rss: RSSConfiguration;
  // twikoo 评论系统配置
  // 参考 https://twikoo.js.org/frontend.html
  twikoo: TwikooConfiguration;
  // 统计相关配置
  tongji: {
    // 百度统计相关配置
    baidu: BaiduTongjiConfiguration[];
  };
  // 微信公众号API相关配置
  wechat: WechatConfiguration;
  // 服务相关配置
  server: ServerConfiguration;
  // 内容资源相关
  resource: ResourceConfiguration;
}

import { getJSON, putJSON } from "./request";

export function getConfiguration() {
  return getJSON("/api/admin/configuration");
}

export function updateWebsiteConfiguration(c: Partial<WebsiteConfiguration>) {
  if (c.setUpTime) {
    c.setUpTime = new Date(c.setUpTime).toISOString();
  } else {
    c.setUpTime = undefined;
  }
  if (c.startRunningTime) {
    c.startRunningTime = new Date(c.startRunningTime).toISOString();
  } else {
    c.startRunningTime = undefined;
  }
  return putJSON("/api/admin/configuration/website", {
    body: JSON.stringify(c),
  });
}
export function updateEmailConfiguration(c: Partial<EmailConfiguration>) {
  return putJSON("/api/admin/configuration/email", { body: JSON.stringify(c) });
}
export function updateUserConfiguration(c: Partial<UserConfiguration>) {
  return putJSON("/api/admin/configuration/user", { body: JSON.stringify(c) });
}
export function updateArticleConfiguration(c: Partial<ArticleConfiguration>) {
  return putJSON("/api/admin/configuration/article", {
    body: JSON.stringify(c),
  });
}
export function updateRSSConfiguration(c: Partial<RSSConfiguration>) {
  return putJSON("/api/admin/configuration/rss", { body: JSON.stringify(c) });
}
export function updateTwikooConfiguration(c: Partial<TwikooConfiguration>) {
  return putJSON("/api/admin/configuration/twikoo", {
    body: JSON.stringify(c),
  });
}
export function updateBaiduTongjiConfigurations(c: BaiduTongjiConfiguration[]) {
  return putJSON("/api/admin/configuration/tongji/baidu", {
    body: JSON.stringify(c),
  });
}
export function updateWechatConfiguration(c: Partial<WechatConfiguration>) {
  return putJSON("/api/admin/configuration/wechat", {
    body: JSON.stringify(c),
  });
}
